package com.feicheng.house.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.house.entity.BuyOrder;
import com.feicheng.house.entity.Deposit;
import com.feicheng.house.entity.Record;
import com.feicheng.house.entity.Yuanshipingzhengbiao;
import com.feicheng.house.exception.SystemException;
import com.feicheng.house.mapper.YuanshipingzhengbiaoMapper;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.IYuanshipingzhengbiaoService;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@Service
public class YuanshipingzhengbiaoServiceImpl extends ServiceImpl<YuanshipingzhengbiaoMapper, Yuanshipingzhengbiao> implements IYuanshipingzhengbiaoService {

	public static final String PAY = "已充值";
	public static final String REFUND = "未充值";
	
	@Autowired
	private YuanshipingzhengbiaoMapper yuanshipingzhengbiaoMapper;
	
	/**
	 * @see.IYuanshipingzhengbiaoService.selectYuanshipingzhengbiao
	 */
	@Override
	public ResultInfo selectYuanshipingzhengbiao(Integer currentPage, Integer pageSize,String searchKey) {
		QueryWrapper<Yuanshipingzhengbiao> yspzWrapper = new QueryWrapper<Yuanshipingzhengbiao>();
		if (null == currentPage) {
			currentPage = 1;
		}
		if (null == pageSize) {
			pageSize = 10;
		}
		if (StringUtils.hasLength(searchKey)) {
			yspzWrapper.and(w ->w.like("out_trade_no", searchKey)
					   .or().like("transaction_id", searchKey)
					   .or().like("bank_type",searchKey)
					   .or().like("create_time", searchKey)
					   .or().like("success_time", searchKey)
					   .or().like("dingdan_type", searchKey)
					   .or().like("openid", searchKey));
		}
		IPage<Yuanshipingzhengbiao> page = new Page<Yuanshipingzhengbiao>(currentPage,pageSize);
		return ResultInfo.success(this.page(page,yspzWrapper));
	}


	/**
	 * @see.IYuanshipingzhengbiaoService.addYuanshipingzhengbiao
	 */
	@Override
	public ResultInfo addYuanshipingzhengbiao(Yuanshipingzhengbiao yuanshipingzhengbiao) {
		QueryWrapper<Yuanshipingzhengbiao> yspzWrapper = new QueryWrapper<Yuanshipingzhengbiao>();
		yspzWrapper.eq("out_trade_no",yuanshipingzhengbiao.getOutTradeNo());
		yspzWrapper.eq("transaction_id",yuanshipingzhengbiao.getTransactionId());
		yspzWrapper.eq("openid", yuanshipingzhengbiao.getOpenid());
		if(yuanshipingzhengbiaoMapper.selectOne(yspzWrapper) != null){
			throw new SystemException("凭证已存在");
		}
		BuyOrder order = new BuyOrder();
		Deposit deposit = new Deposit();
		if ("".equals(yuanshipingzhengbiao.getAppid())) {
			throw new SystemException("应用ID不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getMchid())) {
			throw new SystemException("商户号不能为空");
		} 
		if ("".equals(yuanshipingzhengbiao.getOutTradeNo())) {
			throw new SystemException("商户订单号不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getTransactionId())) {
			throw new SystemException("微信支付单号不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getTradeType())) {
			throw new SystemException("交易类型不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getTradeState())) {
			throw new SystemException("交易状态不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getTradeStateDesc())) {
			throw new SystemException("交易状态描述不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getBankType())) {
			throw new SystemException("付款银行不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getCreateTime())) {
			throw new SystemException("订单完成时间不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getSuccessTime())) {
			throw new SystemException("支付完成时间不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getOpenid())) {
			throw new SystemException("用户标识不能为空");
		}
		if (null == yuanshipingzhengbiao.getTotal()) {
			throw new SystemException("订单总金额不能为空");
		}
		if ("".equals(yuanshipingzhengbiao.getDingdanType())) {
			throw new SystemException("订单类型不能为空");
		}
		if(order.getBuyOrderNumber() == yuanshipingzhengbiao.getOutTradeNo()) {
			yuanshipingzhengbiao.setDingdanType("锻炼");
		}
		else if(PAY.equals(deposit.getDepositState())) {
			yuanshipingzhengbiao.setDingdanType("交押金");
		} 
		else if(REFUND.equals(deposit.getDepositState())) {
			yuanshipingzhengbiao.setDingdanType("退押金");
		}
		else {
			yuanshipingzhengbiao.setDingdanType("办理会员");
		}
		return ResultInfo.success(yuanshipingzhengbiaoMapper.insert(yuanshipingzhengbiao));
	}
}
